package com.bw.gmall.realtime.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.GmallConfig;
import redis.clients.jedis.Jedis;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


public class DimUtil {



/*
1.存redis   String
KEY:   DIM:表名:key  value
*
* */
    public static JSONObject getDimInfo(Connection connection, String tableName, String key) throws InvocationTargetException, SQLException, InstantiationException, IllegalAccessException {
        Jedis jedis = null;
        try {
            //先查询Redis
            jedis = JedisUtil.getJedis();
            String redisKey = "DIM:" + tableName + ":" + key;
            String dimJsonStr = jedis.get(redisKey);

            if (dimJsonStr != null) {
                //重置过期时间
                jedis.expire(redisKey, 24 * 60 * 60);
                //返回维度数据
                return JSON.parseObject(dimJsonStr);
            }

            //拼接SQL语句，确保表名大写以符合Phoenix命名规范
            String querySql = "select * from " + GmallConfig.HBASE_SCHEMA + "." + tableName.toUpperCase() + " where id='" + key + "'";
            //查询数据
            List<JSONObject> queryList = JdbcUtil.queryList(connection, querySql, JSONObject.class, false);

            //检查查询结果是否为空
            if (queryList != null && !queryList.isEmpty()) {
                JSONObject dimInfo = queryList.get(0);
                if (jedis != null) {
                    jedis.set(redisKey, dimInfo.toJSONString());
                    jedis.expire(redisKey, 24 * 60 * 60);
                }
                return dimInfo;
            } else {
                return null;
            }
        } catch (Exception e) {
            // 记录异常但不中断流程
            System.err.println("获取维度数据失败: " + e.getMessage());
            e.printStackTrace();
            return null;
        } finally {
            // 确保关闭Redis连接
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void delDimInfo(String tableName, String key) {
        //获取连接
        Jedis jedis = JedisUtil.getJedis();
        //删除数据
        jedis.del("DIM:" + tableName + ":" + key);
        //归还连接
        jedis.close();
    }


//    public static void main(String[] args) throws Exception {
//        //1.获取连接池
//        DruidDataSource dataSource = DruidDSUtil.createDataSource();  //耗时
//        //2.获取连接
//        DruidPooledConnection connection = dataSource.getConnection();
//
//        //3.调用方法获取维度数据
//        long start = System.currentTimeMillis();
//        JSONObject jsonObject = DimUtil.getDimInfo(connection, "DIM_SKU_INFO", "1");
//        long end = System.currentTimeMillis();
//        //239*6    1300   305  232   233    skuId   spuId    tm_id c1 c2  c3    2
//        //一开始我有关联没有  只关联一个skuId    预聚合操作 数据量   700
//        JSONObject jsonObject2 = DimUtil.getDimInfo(connection, "DIM_SKU_INFO", "1");
//        //12*6    72    每秒   100条
//        long end2 = System.currentTimeMillis();
//
//        System.out.println(end - start);
//        System.out.println(end2 - end);
//
//
//        System.out.println(jsonObject);
//        System.out.println(jsonObject2);
//    }
    public static void main(String[] args) throws Exception {

        DruidDataSource dataSource = DruidDSUtil.createDataSource();

        DruidPooledConnection connection = dataSource.getConnection();

        JSONObject dimInfo = getDimInfo(connection, "DIM_BASE_PROVINCE", "10");

        System.out.println(dimInfo);

        connection.close();




    }

}
